package com.example.kugou.controller;

import com.example.kugou.commons.file.listTest;
import com.example.kugou.pojo.Sing;
import com.example.kugou.pojo.Singer;
import com.example.kugou.service.SingService;
import com.example.kugou.service.SingerService;
import com.example.kugou.vo.ShouyeVO;
import com.example.kugou.vo.SingerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "歌手的相关方法",description = "提供歌手相关的Rest API")
@RequestMapping("/singer")
@RestController
public class SingerController {

    @Resource
    SingerService singerService;

    @Resource
    SingService singService;
    private int id;

    public SingerService getSingerService() {
        return singerService;
    }

    public void setSingerService(SingerService singerService) {
        this.singerService = singerService;
    }

    @GetMapping("/selectSingerId")
    @ApiOperation("根据编号查询歌手的信息")
    public SingerVO selectSingerId(@ApiParam(name = "id",value = "查询的歌手编号"
            ,required = true)int id) {
        SingerVO singerVO = new SingerVO("查询失败",false);
        Singer singer = new Singer();
        try {
            singer = singerService.selectSingerId(id);
            if(singer.getJianjie() != null){
                String fileName = "E:\\小组项目\\网易云\\网易云\\163mHome\\" + singer.getJianjie();
                File file = new File(fileName);
                if (file.exists() && file.isFile()){
                    List<List<String>> lists = new listTest().getSingerInfo(fileName);
                    singerVO.setLists(lists);
                }
            }
            singerVO.setSinger(singer);
            singerVO.setMsg("");
            singerVO.setStatus(true);
        }catch (Exception e){
            singerVO.setMsg(e.getMessage());
            singerVO.setStatus(false);
        }
        return singerVO;
    }

    @ApiOperation("根据类别编号查询全部的歌手")
    @GetMapping("/selectAllSinger")
    public SingerVO selectAllSinger(@ApiParam(name = "typeId",value = "需要查询的编号"
            ,required = false,example = "0")int typeId) {
        SingerVO singerVO = new SingerVO("查询失败",false);
        List<Singer> singers = new ArrayList<>();
        try {
            //查询全部歌手
            singers = singerService.selectAllSinger(typeId, "");
            singerVO.setSingers(singers);
            singerVO.setMsg("");
            singerVO.setStatus(true);
        }catch (Exception e){
            singerVO.setMsg(e.getMessage());
            singerVO.setStatus(false);
        }
        return singerVO;
    }

    @GetMapping("/selectInShouye")
    @ResponseBody
    @ApiOperation("首页查询所用到的全部数据")
    public ShouyeVO selectInShouye() {
        ShouyeVO shouyeVO = new ShouyeVO("查询失败", false);
        List<Sing> sings = new ArrayList<>();
        List<Singer> singers = new ArrayList<>();
        try {
            //查询全部歌手
            singers = singerService.selectAllSinger(0, "");
            shouyeVO.setRuzhuSinger(singers);
            //飙升榜
            sings = singService.SelectByFirstTen();
            shouyeVO.setBiaoshengSings(sings);
            //原创榜
            sings = singService.selectByLastTen();
            shouyeVO.setYuanchuangSings(sings);
            //新歌榜
            sings = singService.SelectByCenterTen();
            shouyeVO.setXingeSings(sings);
            //新歌
            sings = singService.selectNewSings();
            shouyeVO.setNewSings(sings);

            shouyeVO.setMsg("");
            shouyeVO.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            shouyeVO = new ShouyeVO(e.getMessage(), false);
        }
        return shouyeVO;
    }


    @ApiOperation("根据类别查询歌手")
    @GetMapping("/selectBySwid")
    public SingerVO selectBySwid(@RequestParam("swid")Integer swid){
       SingerVO singerVO = new SingerVO();
       List<Singer> singers = singerService.selectBySwid(swid);
       if(singers.size()>0){
           singerVO.setMsg("查询成功");
           singerVO.setStatus(true);
           singerVO.setSingers(singers);
       }else{
           singerVO.setMsg("查询失败");
           singerVO.setStatus(false);
       }
           return singerVO;
    }

    @ApiOperation("根据id删除歌手")
    @RequestMapping("/removeBiRid")
    public SingerVO delete(@RequestParam(value = "rid",required = false)Integer rid){
        SingerVO singerVO = new SingerVO();
        int i = singerService.deleteByRid(rid);
        if(i>0){
            singerVO.setMsg("删除成功");
            singerVO.setStatus(true);
        }else{
            singerVO.setMsg("删除失败");
            singerVO.setStatus(false);
        }
        return singerVO;
    }


    @ApiOperation("根据编号修改歌手简介")
    @RequestMapping("/updateByRid")
    public SingerVO update(@RequestParam("jianjie")String jianjie,@RequestParam("rid")Integer rid){
           SingerVO singerVO = new SingerVO();
           int i = singerService.updateSingerJianjie(jianjie, rid);
           if(i>0){
               singerVO.setMsg("修改成功");
               singerVO.setStatus(true);
           }else{
               singerVO.setMsg("修改失败");
               singerVO.setStatus(false);
           }
           return singerVO;
    }
}
